var skill = {
	save: function() {
		delete this.user_info;
		db.update(db_table_name, this._id, this, true);
	},
	//商家销毁市代
	destroy: function() {
		var bus = BusCon.get(this.bus_id);
		bus._removePurchase(this._id);
		db.remove(db_table_name, this._id)
	},

	setInfo: function(info, is_no_save) {

		// 主题
		this.主题 = String(info.主题 || "").trim();
		if (!this.主题) {
			throwCusError("采购单主题不可为空")
		}
		// 备注
		if (info.hasOwnProperty("备注")) {
			this.备注 = String(info.备注).trim();
		}

		this.cash = 0;
		var materialMap = new Map();
		if (Array.isArray(info.material_list)) {
			var bus = BusCon.get(this.bus_id);
			this.material_list = info.material_list.map((material_item, index) => {
				var res = {
					"厂商": String(material_item.厂商 || "").trim(),
					"品名": String(material_item.品名 || "").trim(),
					"数量": parseInt(material_item.数量) || 0,
					"单位": String(material_item.单位 || "").trim() || "个",
					"单价": parseFloat(material_item.单价) || 0,
				};

				if (!res.品名) {
					throwCusError(`第${index+1}条采购条目品目不可为空`)
				}
				if (!(res.数量 && res.单价)) {
					throwCusError(`第${index+1}条采购条目无法统计总金额`)
				}

				// 将采购材料的信息保存下来，用于智能索引
				var material = bus.buildMaterial(res);
				materialMap.set(material._id, material);
				return Object.assign(res, material)
			});

		}

		// 生成最终价格
		this.cash = this.material_list.reduce((res, material_info) => material_info.单价 * material_info.数量, 0);

		if (info.hasOwnProperty("state") && BusPurchaseCon.STATE.hasOwnValue(info.state = parseInt(info.state))) {
			if (info.state >= 0 && info.state <= info.state) {
				throwCusError("采购单状态不可逆")
			}
			this.state = info.state;
			if (this.state === BusPurchaseCon.STATE.采购中) {
				this.开始采购时间 = +new Date
			} else if (this.state === BusPurchaseCon.STATE.采购完成) {
				this.采购完成时间 = +new Date

				// 添加材料到库存里
				this.material_list.forEach(function(material_info) {
					var material = materialMap.has(material_info._id) ? materialMap.get(material_info._id) : BusMaterialCon.get(material_info._id);
					material._addNum(material_info.数量);
				});
			} else if (this.state === BusPurchaseCon.STATE.采购失败) {
				this.采购失败时间 = +new Date
			}
		}

		is_no_save || this.save();
	}
};

module.exports = skill.multiInherits({});

var db = require("../db/index");
var BusPurchaseCon = require("../bus.purchase/_con");
var BusMaterialCon = require("../bus.material/_con");
var UserCon = require("../user/_con");
var BusCon = require("../bus/_con");
var GoodsCon = require("../goods/_con");
var AchCon = require("../ach/_con");
var BusMACon = require("../bus.MA/_con");
var BusUserBindCon = require("../bus_user_bind/_con");
var BusShopCon = require("../bus.shop/_con");
var BuffCon = require("../buff/_con");
var config = require("../_include/config");
var data_cut = require("../_include/data_cut");
var $Array = require("../_include/$.Array");
var $Object = require("../_include/$.Object");
var helper = require("../_include/helper");
var db_table_name = config.dbname.busPurchase;
var db_id_prefix = config.dbIdPrefix.busPurchase;;